package org.leetcode.middle.leetcode153;

public class Solution {

    public static void main(String[] args) {
        int [] nums = {11,13,15,17};
        Solution solution = new Solution();
        int min = solution.findMin(nums);
        System.out.println(min);
    }


    public int findMin5(int []nums){
        int left = 0;
        int right = nums.length-1;
        if (nums[left]<nums[right]){
            return nums[left];
        }

        while (left<right){
            int mid = (left+right)>>>1;
            if (nums[left]>nums[mid]){
                right=mid;
            }else {
                left=mid+1;
            }
        }

        return nums[left];
    }

    public int findMin3(int[] nums) {

        int left =0;
        int right =nums.length-1;
        while (left<=right){
            int mid =(left+right)>>>1;
            if (nums[left]>nums[mid]){
                right=mid-1;
            }else {
                left=mid+1;
            }
        }

        return nums[left];
    }

    public int findMin2(int[] nums){

        int left =0;
        int right = nums.length-1;

        while (left<right){
            int mid = (left+right)>>>1;
            //最小值一定是在无序中
            if (nums[mid]>nums[right]){
                left=mid+1;
            }else {
                right=mid;
            }
        }
        return nums[left];
    }

    //时间复杂度不符合要求
    public int findMin(int[] nums) {

        int n = nums.length;

        int slow = 0;
        int fast = slow+1;

        for (; fast < n; slow++,fast++) {
            if (nums[fast]<nums[slow])
            {
                return nums[fast];
            }
        }

        return nums[0];
    }


}
